﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace VectorRacer
{
    public class Player
    {
        public delegate void MovedHandler(object sender, int x, int y);

        public event EventHandler Moved;

        private Coordinates _position;
        private PlayerVector _speed = new PlayerVector();

        public Player()
        {
            _position.X = 10;
            _position.Y = 10;
            _speed.X = 0;
            _speed.Y = 0;
        }

        public void Move(int x, int y)
        {
            _speed.X = x - _position.X;
            _speed.Y = y - _position.Y;

            _position.X = x;
            _position.Y = y;

            OnMoved(x, y);
        }

        public ChechMoveResult CheckMove(Coordinates proposedPoint)
        {
            Coordinates resultantPosition = ResultantPosition;

            if (proposedPoint.X < resultantPosition.X - 1 || proposedPoint.X > resultantPosition.X + 1)
                return ChechMoveResult.WrongVector;

            if (proposedPoint.Y < resultantPosition.Y - 1 || proposedPoint.Y > resultantPosition.Y + 1)
                return ChechMoveResult.WrongVector;

            return ChechMoveResult.Correct;
        }

        protected virtual void OnMoved(int x, int y)
        {
            if (Moved != null)
                Moved(this, EventArgs.Empty);
        }

        public Coordinates Position
        {
            get { return _position; }
        }

        public Coordinates ResultantPosition
        {
            get { return new Coordinates(Position.X + Speed.X, Position.Y + Speed.Y); }
        }

        public PlayerVector Speed
        {
            get { return _speed; }
        }
    }
}
